﻿using System;

public class GameGenieDialog
{
    internal GameGenie gg;
    internal MyTableModel model;
    internal NES nes;

    public GameGenieDialog(GameGenie gg, NES nes)
    {
        this.gg = gg;
        this.nes = nes;
        this.initComponents();
    }

    public virtual void destroy()
    {
        this.nes = null;
        this.gg = null;
    }

    private void initComponents()
    {
    }

    internal class MyTableModel
    {
        private GameGenieDialog enclosingInstance;

        public MyTableModel(GameGenieDialog enclosingInstance)
        {
            this.InitBlock(enclosingInstance);
        }

        public int getColumnCount()
        {
            return 4;
        }

        public string getColumnName(int column)
        {
            switch (column)
            {
                case 0:
                    return "Code";

                case 1:
                    return "Address";

                case 2:
                    return "Value";

                case 3:
                    return "Cmp";
            }
            return "-";
        }

        public int getRowCount()
        {
            return (this.Enclosing_Instance.gg.CodeCount + 1);
        }

        public object getValueAt(int row, int column)
        {
            if (row < this.Enclosing_Instance.gg.CodeCount)
            {
                switch (column)
                {
                    case 0:
                        return this.Enclosing_Instance.gg.getCodeString(row).ToUpper();

                    case 1:
                        return ("$" + Misc.hex16(this.Enclosing_Instance.gg.getCodeAddress(row)));

                    case 2:
                        return ("$" + Misc.hex8(this.Enclosing_Instance.gg.getCodeValue(row)));

                    case 3:
                        return ("$" + Misc.hex8(this.Enclosing_Instance.gg.getCodeCompare(row)));
                }
            }
            return "";
        }

        private void InitBlock(GameGenieDialog enclosingInstance)
        {
            this.enclosingInstance = enclosingInstance;
        }

        public bool isCellEditable(int row, int col)
        {
            return (col == 0);
        }

        public void setValueAt(object val, int row, int col)
        {
            if (((val != null) && (val is string)) && !((string) val).Equals(""))
            {
                string newCode = ((string) val).Trim();
                if ((newCode.Length == 6) || (newCode.Length == 8))
                {
                    if (row < this.Enclosing_Instance.gg.CodeCount)
                    {
                        this.Enclosing_Instance.gg.editCode(row, newCode);
                    }
                    else
                    {
                        this.Enclosing_Instance.gg.addCode(newCode);
                    }
                }
            }
            else if (row < this.Enclosing_Instance.gg.CodeCount)
            {
                this.Enclosing_Instance.gg.removeCode(row);
            }
            if (this.Enclosing_Instance.gg.CodeCount > 0)
            {
                this.Enclosing_Instance.nes.GameGenieState = true;
            }
            else
            {
                this.Enclosing_Instance.nes.GameGenieState = false;
            }
        }

        public GameGenieDialog Enclosing_Instance
        {
            get
            {
                return this.enclosingInstance;
            }
        }
    }
}

